'use strict';

const await = require('await-stream-ready/lib/await');
const User = require('../../model/user');

const Controller = require('egg').Controller;
const pageLimit=10;

class FollowsController extends Controller {
    //按页查询关注的用户
    async followerPage() {
        const { ctx,app } = this;
        let page = ctx.params.page ? parseInt(ctx.params.page) : 1;
        let limit=pageLimit;
        let offset=(page-1)*limit;

        let  user_id  = ctx.params.user_id;

        let result;
        result=await app.model.Follows.findAll({
            where: {
                user_id:user_id
            },
            include:[{
                model: app.model.User,
                as:'fol',
                attributes: ['username','avatar','petRelation']
            }],
            attributes: ['follower_id'],
            offset: offset,
            limit: limit
        })
        ctx.apiSuccess(result)
  }
  //按页查询粉丝
  async fanPage() {
    const { ctx,app } = this;
    let page = ctx.params.page ? parseInt(ctx.params.page) : 1;
    let limit=pageLimit;
    let offset=(page-1)*limit;

    let follower_id  = ctx.params.follower_id;

    let result;
    result=await app.model.Follows.findAll({
        where: {
            follower_id:follower_id
        },
        include:[{
            model: app.model.User,
            as:'fan',
            attributes: ['username','avatar','petRelation']
        }],
        attributes: ['user_id'],
        offset: offset,
        limit: limit
    })
    ctx.apiSuccess(result)
 }
 //获取关注列表
 async followerList() {
    const { ctx,app } = this;

    let user_id  = ctx.params.user_id;

    let result;
    result=await app.model.Follows.findAll({
        where: {
            user_id:user_id
        },attributes: ['follower_id']
    })
    ctx.apiSuccess(result)
 }
 //获取粉丝列表
 async fanList() {
    const { ctx,app } = this;

    let follower_id  = ctx.params.follower_id;

    let result;
    result=await app.model.Follows.findAll({
        where: {
            follower_id:follower_id
        },attributes: ['user_id']
    })
    ctx.apiSuccess(result)
 }

 //更改关注状态
 async doFollow() {
    const { ctx,app } = this;

    ctx.validate({
        user_id:{
            type: 'int',
            required: true,
            desc: '用户id'
        },
        follower_id:{
            type: 'int',
            required: true,
            desc: '关注对象id'
        }
    })

    let {user_id,follower_id}=ctx.request.body;

    let data =await app.model.Follows.findOne({
        where: {
            user_id:user_id,
            follower_id:follower_id
        }
    })
    if(!data){
        let record = await app.model.Follows.create({
            user_id:user_id,
            follower_id:follower_id
        });
        ctx.apiSuccess(record)

        // let res={
        //     msg:"对象不存在"
        // };
        // ctx.apiSuccess(res);
    }else{
        let res = await data.destroy();
        ctx.apiSuccess(res);

        // let res={
        //     msg:"对象存在"
        // };
        // ctx.apiSuccess(res);
    }
 }
 //关注计数
 async countFollower(){
    const { ctx,app } = this;

    let user_id  = ctx.params.user_id;

    let result;
    result=await app.model.Follows.count({
        where: {
            user_id:user_id
        }
    })
    ctx.apiSuccess(result)
 }
 //粉丝计数
 async countFan(){
    const { ctx,app } = this;

    let user_id  = ctx.params.user_id;

    let result;
    result=await app.model.Follows.count({
        where: {
            follower_id:user_id
        }
    })
    ctx.apiSuccess(result)
 }

}

module.exports = FollowsController;
